package com.share;

import com.overzealous.remark.Remark;
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
import org.apache.commons.io.IOUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.nio.charset.Charset;

/**
 * @author caifan
 * @since 2022/3/23
 * description:
 */
public class LearningLiang {
    private static final String BASE_URI = "http://learn.lianglianglee.com/";
    private static final String ZHUANLAN = "专栏";
    private static final String ARTICLE = "文章";
    private static final String JIKE = "极客时间";
    private static final String ROOT = "D:/test/learning/";
    private static final String ROOT_DIR = ROOT + ZHUANLAN;

    @Test
    public void getBookTest() throws Exception {
        Connection connect = Jsoup.connect(BASE_URI + URLEncoder.encode(ZHUANLAN, "UTF-8"));
        Document document = connect.get();
        Elements elements = document.getElementsByClass("book-post");
        Elements children = elements.select("div").last().children().last().children();

        String parentDir = null;
        for (Element element : children) {
            if (element.tag().getName().equals("li")) {
                String dir = element.child(0).text();
                parentDir = dir;
                File file = new File(ROOT_DIR + "/" + dir);
                if (!file.exists()) {
                    file.mkdirs();
                }
            }
            if (element.tag().getName().equals("ul")) {
                Elements eleLis = element.select("li");

                for (Element ele : eleLis) {
                    String href = ele.selectFirst("a").attr("href");
                    String fileName = ele.text();
                    File mdFile = new File(ROOT_DIR + "/" + parentDir + "/" + fileName);
                    if (!mdFile.exists()) {
                        mdFile.createNewFile();
                    }
                    String file = convertHtml2Md(href);
                    PrintWriter pw = new PrintWriter(mdFile);
                    pw.println(file);
                    pw.flush();
                    pw.close();
                }

//                element.children().eachText().stream().forEach(System.out::println);
            }

        }
    }

    @Test
    public void urlEncodeTest() throws Exception {
        Connection connect = Jsoup.connect(BASE_URI + URLEncoder.encode("/专栏/12步通关求职面试-完/00 开篇词：了解面试“潜规则”，从海选中脱颖而出.md", "UTF-8"));
        Document document = connect.get();
        Elements elements = document.getElementsByClass("book-post");
        Element last = elements.last();
        String html = last.html();
        html = html.replaceAll("<img src=\"", "<br>![](" + BASE_URI + "专栏/12步通关求职面试-完/");
        html = html.replaceAll("\" alt=\"img\">", ")<br>");
        Remark remark = new Remark();
        String md = remark.convertFragment(html);
        System.out.println(md.replace("\\", ""));
    }

    @Test
    public void singleDir() throws Exception {
        Connection connect = Jsoup.connect(BASE_URI + JIKE);
        Document document = connect.get();
        Elements bookElements = document.getElementsByClass("book-post");
        Elements elements = bookElements.select("div").last().children().last().children();

        for (Element ele : elements) {
            String href = ele.selectFirst("a").attr("href");
            String fileName = ele.text();
            File mdFile = new File(ROOT + "/" + JIKE + "/" + fileName);
            if (!mdFile.exists()) {
                mdFile.createNewFile();
            }
            String file = convertHtml2Md(href);
            PrintWriter pw = new PrintWriter(mdFile);
            pw.println(file);
            pw.flush();
            pw.close();
        }
    }

    private String convertHtml2Md(String href) throws Exception {
        // "专栏/12步通关求职面试-完/00 开篇词：了解面试“潜规则”，从海选中脱颖而出.md"
        Connection connect = Jsoup.connect(BASE_URI + href);
        Document document;
        try {
             document = connect.get();
        } catch (Exception e) {
            System.out.println("parentDir:" + href);
            return "";
        }

        Elements elements = document.getElementsByClass("book-post");
//        Element last = elements.last();elements.
        String html = elements.html();
        html = html.replaceAll("<img src=\"", "<br>![](" + BASE_URI + href.substring(0, href.lastIndexOf("/") + 1));
        html = html.replaceAll("\" alt=\"img\">", ")<br>");
        Remark remark = new Remark();
        String md = remark.convertFragment(html);
        return md.replace("\\", "");
    }


    @Test
    public void html2Md() {
        Remark remark = new Remark();
        String html = "<p><b>测试</b><ul><li>列表1</li><li>列表2</li></ul><p><img src=\"https://static001.geekbang.org/resource/image/cb/55/cb18e672732ab76fc61d60bdf66bf855.png\" alt=\"abc\"></p><a href=\"www.baidu.com\">百度</a></p>";
        String markdown = remark.convertFragment(html);
        System.out.println(markdown);
    }

    @Test
    public void html2Md01() {
        String html = "<p><b>测试</b><ul><li>列表1</li><li>列表2</li></ul><p><img src=\"https://static001.geekbang.org/resource/image/cb/55/cb18e672732ab76fc61d60bdf66bf855.png\" alt=\"abc\"></p><a href=\"www.baidu.com\">百度</a></p>";
        FlexmarkHtmlConverter converter = FlexmarkHtmlConverter.builder().build();
        System.out.println(converter.convert(html));
    }

    @Test
    public void html2Md02() throws Exception {
        String s = IOUtils.toString(new FileInputStream("D:\\geek_md\\go\\Go 语言第一课-白明/15-14-常量：Go在“常量”设计上的创新有哪些？.md"), Charset.defaultCharset());
        FlexmarkHtmlConverter converter = FlexmarkHtmlConverter.builder().build();
        System.out.println(converter.convert(s));
    }

    @Test
    public void html2Md03() throws Exception {
        String s = IOUtils.toString(new FileInputStream("D:\\geek_md\\java并发编程实战-王宝令/03-01-可见性、原子性和有序性问题：并发编程Bug的源头.md"), Charset.defaultCharset());
        FlexmarkHtmlConverter converter = FlexmarkHtmlConverter.builder().build();
        String content = converter.convert(s).replace("\\", "");
        System.out.println(content);
    }

}
